﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars.Localization;

namespace MS.INV
{
    public partial class SMain : DevExpress.XtraEditors.XtraForm
    {
        Lib.Update.Client _uClient = new Lib.Update.Client();

        public SMain()
        {
            InitializeComponent();

            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.Skins.SkinManager.EnableFormSkins();

            defaultLookAndFeel1.LookAndFeel.StyleChanged += new EventHandler(LookAndFeel_StyleChanged);
            if (System.IO.File.Exists(Application.StartupPath + @"\Skins.xml"))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(Application.StartupPath + @"\Skins.xml");
                if (ds != null && ds.Tables.Contains("Skins"))
                {
                    DataTable DT = ds.Tables["Skins"];
                    defaultLookAndFeel1.LookAndFeel.SkinName = DT.Rows[0]["Name"].ToString();
                }
            }

            //_uClient.ProgressChanged += new Lib.Update.Client.ProgressChangedHandler(_uClient_ProgressChanged);
            //_uClient.RunUpdate();

            load_config.RunWorkerAsync();
        }

        void LookAndFeel_StyleChanged(object sender, EventArgs e)
        {
            DataTable DT = new DataTable("Skins");
            DT.Columns.Add("Name", Type.GetType("System.String"));
            DataRow row = DT.NewRow();
            row["Name"] = this.LookAndFeel.ActiveSkinName;
            DT.Rows.Add(row);

            DataSet ds = new DataSet("Skins");
            ds.Tables.Add(DT);
            ds.WriteXml(Application.StartupPath + @"\Skins.xml");
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Lib.Bases.Developer.Sys_Developer dev = new Lib.Bases.Developer.Sys_Developer();
            dev.StartPosition = FormStartPosition.CenterScreen;
            dev.ShowDialog();
        }

        private void SMain_Load(object sender, EventArgs e)
        {
            Monitor ff = new Monitor();
            mMC.Controls.Add(ff);
            ff.Dock = DockStyle.Fill;
            ff.Show();

            MReport rpt = new MReport();
            mvRpt.Controls.Add(rpt);
            rpt.Dock = DockStyle.Fill;
            rpt.Show();

            MHome home = new MHome();
            mvHome.Controls.Add(home);
            home.Dock = DockStyle.Fill;
            home.Show();

            MSystem _sy = new MSystem();
            _sy.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            _sy.TopLevel = false;
            mvSys.Controls.Add(_sy);
            _sy.Dock = DockStyle.Fill;
            _sy.Show();

            viewControl.SelectedTab = mnHome;

            lblSrv.Caption = Lib.Data.ConnInfo.ServerName + ", " + Lib.Data.ConnInfo.DatabaseName;

            LibExtend.Config.AllowChangePrice = true;

            BarLocalizer.Active = new MyBarLocalizer();
            
            SkinHelper.InitSkinPopupMenu(barSkins);

            Systems.WebComClient web = new Systems.WebComClient();
            web.Start();
        }

        //void down_ProgressChanged(Lib.Update.Downloader.DownSender sender)
        //{
        //    barDownload.EditValue = sender.ProgressPercentage;
        //    if (sender.DownloadCompleted)
        //    {
        //        lblDown.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        //        barDownload.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

        //        Lib.Update.UpdateForm ff = new Lib.Update.UpdateForm();
        //        ff.TopMost = true;
        //        ff.Show();
        //    }
        //}

        private void btAbout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            About ff = new About();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.ShowDialog();
        }

        private void test1_SupportClicked(object sender, EventArgs e)
        {
            Lib.Com.SupportOnl ff = new Lib.Com.SupportOnl();
            ff.StartPosition = FormStartPosition.CenterScreen;
            ff.Show();
        }

        private void load_config_DoWork(object sender, DoWorkEventArgs e)
        {
            Lib.Bases.Configs.LocalXml.ReadXml();
            LibExtend.Config.PrinterName = Lib.Bases.Configs.LocalXml.PrinterName;
        }
    }

    // Custom localizer that changes skin captions 
    public class MyBarLocalizer : BarLocalizer
    {
        public override string GetLocalizedString(BarString id)
        {
            if (id == BarString.SkinCaptions)
            {
                //Default value for BarString.SkinCaptions:
                //"|DevExpress Style|Caramel|Money Twins|DevExpress Dark Style|iMaginary|Lilian|Black|Blue|Office 2010 Blue|Office 2010 Black|Office 2010 Silver|Office 2007 Blue|Office 2007 Black|Office 2007 Silver|Office 2007 Green|Office 2007 Pink|Seven|Seven Classic|Darkroom|McSkin|Sharp|Sharp Plus|Foggy|Dark Side|Xmas (Blue)|Springtime|Summer|Pumpkin|Valentine|Stardust|Coffee|Glass Oceans|High Contrast|Liquid Sky|London Liquid Sky|The Asphalt World|Blueprint|"
                string defaultSkinCaptions = base.GetLocalizedString(id);
                string newSkinCaptions = defaultSkinCaptions.Replace("|DevExpress Style|", "|My Favorite Skin|");
                return newSkinCaptions;
            }
            return base.GetLocalizedString(id);
        }
    }
}
